package cn.yiyuan.service.impl;


import cn.yiyuan.base.context.LoginContext;
import cn.yiyuan.domain.Employee;
import cn.yiyuan.service.IEmployeeService;
import cn.yiyuan.service.ILonginService;
import cn.yiyuan.vo.LoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Objects;
import java.util.UUID;

/**
 * @user m
 * @project system_boog
 * @date 2023-10-17
 * @package cn.yiyuan.service.impl
 */
@Service
public class ILonginServiceImpl implements ILonginService {

   @Autowired
   private IEmployeeService employeeService;


    @Override
    public LoginVO login(Employee employee) {
//        数据判空
        if (Objects.isNull(employee.getUsername())
                || Objects.isNull(employee.getPassword())
        )throw new RuntimeException("客官，账号密码不正确，请在试试哦");
//        根据账户查密码
       Employee user = employeeService.queryByName(employee);
       if (Objects.isNull(user))throw new RuntimeException("客官，账号密码不正确，请在试试哦");
//        判断密码是否正确
       if(!user.getPassword().equals(employee.getPassword()))throw new RuntimeException("账号或密码不正确");
//        将用户信息存map  以后存redis
       String token = UUID.randomUUID().toString();
//        生成UUID返回
       LoginContext.loginMap.put(token,user);
       LoginVO loginVO = new LoginVO();
       loginVO.setToken(token);
       // 这个user是不是要返给前端? 有没有什么不妥的地方
       // 要进行数据脱敏
       user.setPassword(null);  //判空
//       user.setId(null); 判空
//       user.setEmail(null);
       loginVO.setUser(user);
       return loginVO;
    }

}
